package lib.pacificapp.app;
import java.lang.ref.WeakReference;
  
import lib.pacificapp.util.ICookie;
import android.annotation.SuppressLint;
import android.app.Activity; 
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public abstract class PragmentAction  extends FragmentActivity implements ActionInter{

	@Override
	public abstract int getLayoutViewId();
	@Override
	public abstract int getModeCode() ;

	@Override
	public abstract boolean initView(View parentView);
	
	protected abstract void onCreateCompeleted(View parent);
	 @Override
	 public void onCreate(Bundle icicle) {
	         super.onCreate(icicle);
	         setContentView(getLayoutViewId());
	         initView(null);
	         onCreateCompeleted(null);
	  }
	 
	 
	 /***same width PaFrament this s*/
	 protected boolean text(int rId,String str){
		 TextView tv=(TextView)findViewById(rId);
		 if(tv==null)return false;
		 tv.setText(str);
		 return true;
	  }
	 
	//记录用户首次点击返回键的时间
	 private long firstStartTime=0;
    protected void start(Class<? extends Activity> action){
	 long secondTime=System.currentTimeMillis();
     if(secondTime-firstStartTime<1000){
     	 
     }else{
    	 Intent intent = new Intent( this,action);
	         startActivity(intent);
     }
     firstStartTime=secondTime;
  }
	 
	  protected ICookie cookie(String key){
		  return new ICookie(this,key);
	  }
	  protected ICookie cookie(int key){
		  return new ICookie(this,String.valueOf(key));
	  }
	  protected  int ALERT=0x0001;
	  protected  int START=0x0002; 
	       protected void sendMessage(int code,Object data){ 
	    	   Message msg=new Message();
	           msg.obj=data;
	           msg.what=code; 
	           msgHandler.sendMessage(msg);
	       }	
	       protected void sendMessage(int code){
	    	   sendMessage(code,null);
	       }	
	     
	       @SuppressWarnings("unchecked")
	       protected void onSendMessageCompleted(int code,Object data){
	    	    /**回调*/
	    	    if(code==ALERT){
	    	    	Toast.makeText(getApplicationContext(),String.valueOf(data),
	    	    		      Toast.LENGTH_SHORT).show();
	    	    }else if(code==START){
	    	    	 start((Class<? extends Activity>)data);
	    	    }
	       }
	       
	       protected void start(final Class<? extends Activity> action,int delay){
	    	     new Handler().postDelayed(new Runnable(){    
	    		    public void run() {    
	    		        sendMessage(START,action);
	    		    }    
	    		 }, delay);    
	       }
	       @SuppressLint("HandlerLeak")
			Handler msgHandler = new Handler(){
			            @Override
				            public void handleMessage(Message msg){
			            	onSendMessageCompleted(msg.what,msg.obj);
				           }
				 };
	       @Override  
	       public void onDestroy() {  
	           //  If null, all callbacks and messages will be removed.  
	    	   msgHandler.removeCallbacksAndMessages(null);  
	           super.onDestroy();
	       }  
	   
//	       private final Handler mHandler = new MyHandler(this ); 
//	       private static class MyHandler extends Handler {  
//	           private final WeakReference<PragmentAction> mActivity;  
//	           
//	           public MyHandler(PragmentAction activity) {  
//	               mActivity = new WeakReference<PragmentAction>(activity);
//	              
//	           }  
//	     
//	           @Override  
//	           public void handleMessage(Message msg) {  
//	        	   if( mActivity.get()==null)return;
//	        	   mActivity.get().onSendMessageCompleted(msg.what, msg.obj);
//	           }  
//	       }  
}
